package trans.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import trans.action.support.PageMode;
import trans.model.User;
import trans.service.UserDAO;
import trans.support.BasicSupport;
import trans.support.Constants;

import com.opensymphony.xwork2.Preparable;

public class UserMgrAction extends BasicSupport implements Preparable{
	
	private static final String SHOW_USER_TIPS = "ShowUserTips";

	private static final long serialVersionUID = 1L;
	
	private User user;
	private Map<Integer,String> roles;
	
	@Override
	public void prepare() throws Exception {
		initManuShow();
		roles = Constants.USER_ROLES;
	}
	
	public String search() throws Exception{
		PageMode pm = getPageMode(request);
		UserDAO.fillUserPage(pm);
		initPager(pm);
		
		
		return "userSearch";
	}
	
	public String userCreate() throws Exception{
		return "userEdit";
	}
	
	
	private File upload;
	private String uploadFileName;
	public String save() throws Exception{
		boolean isUpdate = (user.getId() != null);
		uploadIcon();
		boolean result = UserDAO.saveOrUpdateUser(user);
		
		if(!result){
			addActionError(getText("message.user.save.error.exist"));
		}else{
			//reload User for getting usrTip after update user
			user = UserDAO.getUser(user.getId());
			if(isUpdate){
				addActionMessage(getText("message.user.update.success"));
			}else{
				addActionMessage(getText("message.user.create.success"));
			}
		}
		
		if(user.getIsCustomer() && currentUser().getIsCustomer() == false){
			request.setAttribute(SHOW_USER_TIPS, true);
		}
		
		return "userEdit";
	}

	@SuppressWarnings("deprecation")
	private void uploadIcon() throws IOException, Exception {
		if(upload == null){
			return;
		}
		
		InputStream is = null;
		OutputStream os = null;
		try {
			 is = new FileInputStream(upload);
			 String path = request.getRealPath("/")+ "/img/userIcon/";
			 String fileName = user.getUserCode() + "." + uploadFileName.split("\\.")[1];
			 user.setIcon(fileName);
			 
			 os = new FileOutputStream(new File(path+fileName));
			 byte[] buf = new byte[1024];
			int len = -1;
			while ((len = is.read(buf)) != -1) {
				os.write(buf, 0, len);
			}
			os.flush();
			is.close();
			os.close();
		} catch (Exception e) {
			System.out.println("upload file error :" + e.getMessage());
			is.close();
			os.close();
			throw  e;
		}
		//delete tempFile
		upload.delete();
	}
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}


	private Integer editUserId;
	private Integer updateStatus;
	public String updateUserStatus() throws Exception{
		UserDAO.updateUserStatus(editUserId, updateStatus);
		addActionMessage(getText("message.sa.update.success"));
		return search();
	}
	
	public String editUser()  throws Exception{
		user = UserDAO.getUser(editUserId);
		
		if(user.getIsCustomer()){
			request.setAttribute(SHOW_USER_TIPS, true);
		}
		return "userEdit";
	}
	
	public String editAccount() throws Exception{
		user = currentUser();
		return "userEdit";
	}
	
	private Integer deleteTipId;
	public String deleteTip() throws Exception{
		UserDAO.deleteTip(deleteTipId);
		addActionMessage(getText("message.user.update.success"));
		return editUser();
	}
	
	public String sop() throws Exception{
		request.setAttribute("sop", currentUser().getSop());
		return "sop";
	}
	
	
	private Integer clientId;
	private Integer saId;
	public String viewSop() throws Exception{
		user = UserDAO.getUser(clientId);
		
		request.setAttribute("sop", user.getSop());
		request.setAttribute("saId", saId);
		return "sop";
	}
	
	public Integer getClientId() {
		return clientId;
	}

	public void setClientId(Integer clientId) {
		this.clientId = clientId;
	}

	public Integer getSaId() {
		return saId;
	}

	public void setSaId(Integer saId) {
		this.saId = saId;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Map<Integer, String> getRoles() {
		return roles;
	}

	public void setRoles(Map<Integer, String> roles) {
		this.roles = roles;
	}

	public Integer getEditUserId() {
		return editUserId;
	}

	public void setEditUserId(Integer editUserId) {
		this.editUserId = editUserId;
	}

	public Integer getUpdateStatus() {
		return updateStatus;
	}

	public void setUpdateStatus(Integer updateStatus) {
		this.updateStatus = updateStatus;
	}

	public Integer getDeleteTipId() {
		return deleteTipId;
	}

	public void setDeleteTipId(Integer deleteTipId) {
		this.deleteTipId = deleteTipId;
	}

}
